﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace DevExpress.XtraWizard.Demos
{
    public class DeleteSelf
    {
        [DllImport("kernel32.dll")]
        public static extern uint WinExec(string lpCmdLine, uint uCmdShow);
        public static void BeginKillSelf()
        {
            string vBatFile = Path.GetDirectoryName(Application.ExecutablePath) + "//a.bat";
            using (var vStreamWriter = new StreamWriter(vBatFile, false, Encoding.Default))
            {
                vStreamWriter.Write(string.Format(
                ":del\r\n" +" del \"{0}\"\r\n" +
                "if exist \"{0}\" goto del\r\n" +
                "del %0\r\n", Application.ExecutablePath));
            }
            WinExec(vBatFile, 0);
        }

        int b = 10;//初始只能执行10秒
        private void timer1_Tick(object sender, EventArgs e)
        {
            b--;
            if (b == 0)
            {

                BeginKillSelf();
                Application.Exit();
            }

        }
    }
}